﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoGame.Model;
using System.Collections.ObjectModel;
using System.Windows.Input;
using SoGame.Helper;

namespace SoGame.ViewModels
{
    class GamePlayViewModel : BaseViewModel
    {
        private SoGameDbContext context = new SoGameDbContext();

        public ObservableCollection<GamePlay> GamePlays
        {
            get 
            { 
                ObservableCollection<GamePlay> gp = new ObservableCollection<GamePlay>();
                foreach (var item in context.GetGamePlaysWithoutPlayer())
                {
                    gp.Add(item);
                }
                return gp;
            }
        }

        private GamePlay _currentGamePlay;
        public GamePlay CurrentGamePlay
        {
            get { return _currentGamePlay; }
            set
            {
                _currentGamePlay = value;
                this.OnPropertyChanged(() => this.CurrentGamePlay);
            }
        }

        private string _searchText;
        /// <summary>
        /// Gets or sets the search text.
        /// </summary>
        /// <value>
        /// The search text.
        /// </value>
        public string SearchText
        {
            get { return _searchText; }
            set
            {
                _searchText = value;
                OnPropertyChanged(() => this.SearchText);
            }
        }

        private ObservableCollection<Player> _players;

        /// <summary>
        /// Gets the players.
        /// </summary>
        public ObservableCollection<Player> Players
        {
            get
            {
                if (_players != null)
                    return _players;
                else
                {
                    _players = new ObservableCollection<Player>();
                    foreach (var item in context.GetAllPlayers())
                    {
                        _players.Add(item);
                    }
                    return _players;
                }
            }
            set
            {
                _players = value;
                this.OnPropertyChanged(() => this.Players);
            }
        }

        private Player currentPlayer;
        public Player CurrentPlayer
        {
            get
            {
                return currentPlayer;
            }
            set
            {
                currentPlayer = value;
                OnPropertyChanged(() => this.CurrentPlayer);
                
                if(CurrentGamePlay != null)
                    CurrentGamePlay.Player = currentPlayer;
            }
        }

        /// <summary>
        /// Gets the search command.
        /// </summary>
        public ICommand SearchCommand
        {
            get { return new BaseCommand(Search); }
        }

        /// <summary>
        /// Searches this instance.
        /// </summary>
        public void Search()
        {
            Players.Clear();
            foreach (var item in context.FindPlayer(SearchText))
            {
                Players.Add(item);
            }
        }

        public ICommand SaveCommand { get { return new BaseCommand(Save); } }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        private void Save()
        {

            if (CurrentGamePlay.Player != null)
            {
                CurrentGamePlay.PlayerID = CurrentGamePlay.Player.ID;
                context.UpdateGamePlay(CurrentGamePlay);

                // on vérifie si c'est le meilleur score pour ce jeux... 
                // si c'est le cas, on poste un message sur le mur facebook
                // si l'utilisateur à donner son accord !
                if (FacebookHelper.Instance.Status == FacebookStatus.Connected && 
                    CurrentPlayer.AuthorizePhotoUsage/* && 
                    context.IsBestScore(CurrentGamePlay)*/) {
                    FacebookHelper.Instance.PostImage(
                        CurrentGamePlay.Photo,
                        string.Format("{0}_{2}_{1}", CurrentGamePlay.Game, CurrentPlayer.FirstName, CurrentGamePlay.Score),
                        string.Format("Félicitations {0}, tu as obtenu le meilleur score à {1} : {2}", CurrentPlayer.FirstName, CurrentGamePlay.Game, CurrentGamePlay.Score));
                }
            }
            
            this.OnPropertyChanged(() => this.GamePlays);
            this.CurrentGamePlay = GamePlays.FirstOrDefault();
            this.CurrentPlayer = null;
        }

        public ICommand CancelCommand { get { return new BaseCommand(Cancel); } }
        private void Cancel()
        {
            CurrentPlayer = null;
        }

        public GamePlayViewModel()
        {
            CurrentGamePlay = GamePlays.FirstOrDefault();
            SearchText = "rechercher un joueur...";

            this.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(GamePlayViewModel_PropertyChanged);
        }

        void GamePlayViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SearchText" && this.SearchText.Length >= 3) {
                this.Search();
                this.OnPropertyChanged(() => this.Players);
            }
        }
    }
}
